<?php

class Fan_IndexController extends Core_Controller_Action_Standard
{
  public function init()
  {
  
    // @todo this may not work with some of the content stuff in here, double-check
    $subject = null;
    if( !Engine_Api::_()->core()->hasSubject() )
    {
      $id = $this->_getParam('id');
    
      /* TESTING ID HERE
    echo "m i here";
    echo $id;exit;   */
      // use viewer ID if not specified
      //if( is_null($id) )
      //  $id = Engine_Api::_()->user()->getViewer()->getIdentity();

      if( null !== $id )
      {
        $subject = Engine_Api::_()->user()->getUser($id);
        if( $subject->getIdentity() )
        {
          Engine_Api::_()->core()->setSubject($subject);
        }
      } 
    }
  }
  public function indexAction()
  {
   // $this->view->someVar = 'someVal';
       //$this->view->someVar = 'someVal';
   if( !$this->_helper->requireUser()->isValid() ) return;
   
    $subject = Engine_Api::_()->core()->getSubject();
    $viewer = Engine_Api::_()->user()->getViewer();
    $isFan = Engine_Api::_()->fan()->isFan($subject);
    
    if($isFan instanceof Fan_Model_Fan) {
      return $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => true,
        'parentRefresh' => true,
        'messages' => array(Zend_Registry::get('Zend_Translate')->_('You are already friends with this member.'))
      ));
    }
    
      $this->view->form = $form = new Fan_Form_Follow();
      
      if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) ) {

      $table = Engine_Api::_()->getDbTable('fans','fan');
      $values = $form->getValues();
      $viewer = Engine_Api::_()->user()->getViewer();

      // Begin database transaction
      $db = $table->getAdapter();
      $db->beginTransaction();

      try
      {
        $result = $table->createRow();
        $result->setFromArray($values);
        $result->follower_id = $viewer->getIdentity();
        $result['resource_id'] = $subject->getIdentity();
        //$result['resource_type'] = $subject->getType();
        $viewer = Engine_Api::_()->user()->getViewer();
        $result->save();

        //activity feed
		$api = Engine_Api::_()->getDbtable('actions', 'activity');
		$action = $api->addActivity(Engine_Api::_()->getItem('user', $result->follower_id), Engine_Api::_()->getItem('user', $result->resource_id), 'fan_new');
      
        $db->commit();
      }

      catch( Exception $e )
      {
        $db->rollBack();
        throw $e;
        
      }
      /* This Code is for Redirectly On to the page with a POP _UP */
      return $this->_forward('success' ,'utility', 'core', array(
          'smoothboxClose' => true,
          'parentRefresh' => true,//Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'group_general', true),
          'messages' => Array('Now You Are a Fan.')
        ));
    }
  }
  
  
  
  
  
  
  //////////////////////////////////////////////
  
  public function removeAction(){
        
      $viewer = Engine_Api::_()->user()->getViewer();
      
      $fan_id = $this->_getParam('id');
      
      $this->view->form = $form = new Fan_Form_Follow();
      
      $form->setTitle('Undo Fan');
      $form->setDescription('Are you sure you want to remove your self from this user\'s fans list?');
      $form->getElement('submit')->setOptions(array('label' => 'Remove'));
      
      if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) ) {
          // This Code is For Deletion for Item   
       try {
           $fan = Engine_Api::_()->getItem('fan',$fan_id);
           if(!$fan instanceof Fan_Model_Fan){
               return $this->_forward('success' ,'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => true,//Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'group_general', true),
                'messages' => Array('Something went wrong. Try again after some time.')
                ));
           }
           $fan->delete();
           return $this->_forward('success' ,'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => true,//Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'group_general', true),
                'messages' => Array('You are not fan of this user')
                ));
       } catch(Exception $e){
           throw $e;
       }
      }
      
       
    }
  
  
  
  ///////////////////////////
  
  
}
